package com.lims.utils;

/**
 * 检查GB11643-1999标准的身份证号
 * @author eshore
 *
 */
public class IDCardValidate {

	/**
	 * 是否合法的身份证
	 * @param idcard
	 * @return
	 */
	public static boolean validate18Idcard(String idcard){
		//符合GB11643-1999标准
		String address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
        if(idcard == null ) {
            return false;
        }
        if(idcard.length()!=18) {
            return false;
        }
        String prefix=idcard.substring(0,2);
        if(address.indexOf(prefix)<0)return false;
        char [] id =idcard.toCharArray();
        int i, sum, n;
        for (sum = i = 0; i < 17; i++){
            sum += ((1 << (17 - i)) % 11) * (id[i] - '0');
        }
        n = (12 - (sum % 11)) % 11;
        if (n < 10) {
            return (n == id[17] - '0');
        } else {
            return (id[17] == 'X'||id[17] == 'x');
        }
    }
}
